package com.pishou.code.util;

import com.pishou.Data;
import com.pishou.ParamConfig;
import com.pishou.URLConfig;
import com.pishou.code.bean.Video;
import com.pishou.code.csv.PlayListCSV;
import com.pishou.code.thread.UpdatePlayList;
import com.pishou.code.thread.VideoDownload;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class VideoPlayList {

    // 广告播放列表
    private static List<Video> video_list = new ArrayList<>();
    // 当前播放广告的索引
    private static int next_index = 0;
    // 当前播放的是什么类别视频  Ad：广告    Dial：点播
    public static String target = "Ad";
    // 点播列表
    public static List<String> video_Dial = new ArrayList<>();

    private static int start_download_times = 0;

    public static void video_list_to_next() {
        if(video_list.size()==0) {
            video_list = PlayListCSV.getPlayList();
        }else {
            next_index++;
            if (next_index >= video_list.size()) {
                next_index = 0;
            }
        }
        if(video_list.size()<=0) {
            Thread t = new UpdatePlayList(ParamConfig.getConfig_json().getInteger("playerId"));
            t.start();
            while (t.isAlive());
        }
    }

    public static String getNextVideoPath() {
        String path = "";
        if(video_Dial.size()>0) {
            // 如果点播列表中有数据，则播放点播列表的
            // 获取列表的第一个，获取后从列表中删除
            // 设置播放类别为点播
            path = video_Dial.get(0);
            video_Dial.remove(0);
            target = "Dial";
        }else {
            video_list_to_next();
            path = video_list.get(next_index).getPath();
            target = "Ad";
        }
        File file = new File(path);
        while (!file.exists()) {
            start_download_times++;
            System.out.println("节目：《"+video_list.get(next_index).getName()+"》，文件不存在，启动下载！");
            // 启动OSS下载任务
            new VideoDownload(video_list.get(next_index)).start();
            // 索引 +1
            next_index++;
            if (next_index >= video_list.size()) {
                next_index = 0;
            }
            path = video_list.get(next_index).getPath();
            file = new File(path);
            System.out.println(new Date()+"播放节目：《"+video_list.get(next_index).getName()+"》");
            // 初次加载程序时，没有任何视频可供播放。为避免程序进入疯狂循环，判定所有视频启动下载时，程序休眠5秒钟，留出时间作为下载视频使用
            if (start_download_times >= video_list.size()){
                start_download_times = 0;
                try{
                    Thread.sleep(5000);
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }
        start_download_times = 0;
        if("Ad".equals(target)) {
            System.out.println(new Date()+"播放节目：《"+video_list.get(next_index).getName()+"》");
        }else {
            System.out.println(new Date()+"播放节目：《点播》");
        }
        return path;
    }

    public static void setVideo_list(List<Video> list) {
        // 修改静态变量中的播放列表
        VideoPlayList.video_list = list;
        // 修改硬盘中保存的播放列表
        PlayListCSV.setPlayList(list);
    }

    public static Video getCurrentVideo(){
        Video video = video_list.get(next_index);
        if (video.getCommentId() == null || Data.getProduct(video.getSku()) == null){
            return  null;
        }
        File file = new File(video.getCommentVideoPath());
        if (!file.exists()){
            Video v = new Video();
            v.setOss(URLConfig.oss_host + "Buying_show/" + video.getCommentId() + ".mp4");
            v.setPath(video.getCommentVideoPath());
            v.setName("Comment:" + video.getCommentId() + ".mp4");
            new VideoDownload(v).start();
            return null;
        }
        return  video;
    }

}
